package com.blac

import android.app.Activity
import android.app.Application
import android.app.Application.ActivityLifecycleCallbacks
import android.content.Context
import android.os.Bundle
import com.applovin.sdk.AppLovinMediationProvider
import com.applovin.sdk.AppLovinPrivacySettings
import com.applovin.sdk.AppLovinSdk
import com.applovin.sdk.AppLovinSdkInitializationConfiguration
import com.blac.ad.MaxReward
import com.blac.utils.ToastUtil

object SDKManager : ActivityLifecycleCallbacks{

    private val instanceHolder by lazy { SDKManager }

    fun getInstance(): SDKManager = instanceHolder
    lateinit var mApplication:Context

    fun init(activity: Activity){
        mApplication =activity.applicationContext
        registerActivityLifecycleCallbacks(mApplication)
        Business.init(mApplication)

//        initAd(mApplication)
        ToastUtil.init(activity)
    }

    fun initAd(mApplication: Context?) {
        mApplication?.let {
            AppLovinPrivacySettings.setHasUserConsent(true,it)
            AppLovinPrivacySettings.setDoNotSell(false,it)

            val initConfig = AppLovinSdkInitializationConfiguration
                .builder(SDK.decrypt(Constant.MAX_KEY),it)
                .setMediationProvider(AppLovinMediationProvider.MAX)
                .build()
            AppLovinSdk.getInstance(it).initialize(initConfig) {sdkConfig->
//            AppLovinSdk.getInstance(mApplication).showMediationDebugger()
                mApplication?.let { it1 -> MaxReward.loadRewardAd(it1) }
            }
        }

    }

    private fun registerActivityLifecycleCallbacks(mApplication: Context?) {
        (mApplication as Application).registerActivityLifecycleCallbacks(this)
    }

    override fun onActivityCreated(p0: Activity, p1: Bundle?) {
        Business.onActivityCreate(p0)
    }

    override fun onActivityStarted(p0: Activity) {

    }

    override fun onActivityResumed(p0: Activity) {

    }

    override fun onActivityPaused(p0: Activity) {
    }

    override fun onActivityStopped(p0: Activity) {

    }

    override fun onActivitySaveInstanceState(p0: Activity, p1: Bundle) {

    }

    override fun onActivityDestroyed(p0: Activity) {
    }

}